<?php
/*
Name: Carbon Paper Post
Version: 0.1
Author: Bertrand Scarpa
Description: Increase gradually user votes for any posts following goals defined by writer 
Use a distribution profile (Gumbel's law)  
*/

include_once('class-vote.php');
include_once(INCLUDEPATH . 'class-date.php');
class CPost{
	
	private $ID;
	private $post_title;
	private $post_type;
	private $post_date;
	private	$scope;
	private $session;
	
	public function __construct($post_ID){
		$this->ID = $post_ID;
	}

	public function set_ID($ID){
		$this->ID = $ID;
	}

	public function get_ID(){
		return $this->ID;
	}

	public function set_title($post_title){
		$this->post_title = $post_title;
	}
	
	public function get_title(){
		return $this->post_title;
	}

	public function set_type($post_type){
		$this->post_type = $post_type;
	}

	public function get_type(){
		return $this->post_type;
	}
	
	public function set_date($post_date){
		$this->post_date = $post_date;
	}
	
	
	public function get_date(){
		return $this->post_date;
	}


	public function set_scope(){
		// Define the scope of sessions
		$dNow = new Datetime('now');
	 	$dDate = new Datetime ($this->post_date);
		$diff = date_diff($dNow, $dDate);
		$elapsed_jobs = $diff->format('%d') * 24 + $diff->format('%h');
		$this->scope = array_sum(array_slice (CProfiler::$jobs, CProfiler::$duration - $elapsed_jobs, CProfiler::$duration));
	}

	public function get_scope(){
		return $this->scope;
	}

	public function set_session(){
		// Define the current session
		$dNow = new Datetime('now');
	 	$dDate = new Datetime ($this->post_date);
		$diff = date_diff($dNow, $dDate);
		$elapsed_jobs = $diff->format('%d') * 24 + $diff->format('%h');
		$this->session = array_sum(array_slice (CProfiler::$jobs, CProfiler::$duration - $elapsed_jobs, $elapsed_jobs));
	}	

	public function get_session(){
		return $this->session;
	}

	public function get_votes(){
		global $wpdb;
		// Get global votes
		$rVotes = $wpdb->get_var("SELECT meta_value from {$wpdb->postmeta} where post_id={$this->ID} and meta_key = '_metabox_voteprofiler'");
		$aVotes = unserialize($rVotes);
		if($aVotes){
			foreach ($aVotes as $ID => $goal){
				$oVote = new CVote($ID,$this->ID,$this->scope, $this->session, $goal);
				$votes[] = $oVote;
			}
		}		
		return $votes;
	}
	
	public function get_vote_by_ID($ID){
		global $wpdb;
		$rVotes = $wpdb->get_var("SELECT meta_value from {$wpdb->postmeta} where post_id={$this->ID} and meta_key = '_metabox_voteprofiler'");
		$aVotes = unserialize($rVotes);
		foreach ($aVotes as $vote_ID => $goal){
			if ($vote_ID == $ID)
				$oVote = new CVote($ID,$this->ID,$this->scope, $this->session, $goal);
		}		
		return $oVote;
	}
}
?>